package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {




    @Autowired
    ShoppingCartService shoppingCartService;

    @Autowired
    DishService dishService;

    @Autowired
    CategoryService categoryService;

    @Autowired
    DishFlavorService dishFlavorService;

    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        //构造查询条件
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();

      queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
      queryWrapper.orderByDesc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
        return R.success(list);
    }


    @DeleteMapping("/clean")
    public R<String> deleById(){
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());

        shoppingCartService.remove(queryWrapper);

        return R.success("删除成功");
    }





    @PostMapping("/add")
    public R<String> add(@RequestBody ShoppingCart shoppingCart){
        //获得选中的购物车
        ShoppingCart one1 = getOne1(shoppingCart);
        //判空
        //空：数量为1
        if (one1==null){
            shoppingCart.setNumber(1);
            shoppingCartService.save(shoppingCart);
        //不为空：数量+1
        }else {
            one1.setNumber(one1.getNumber()+1);
            shoppingCartService.updateById(one1);

        }

        return R.success("ok");
    }


    @PostMapping("/sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart){
        //获得选中的购物车
        ShoppingCart one1 = getOne1(shoppingCart);
        Integer number = one1.getNumber();
        //获得里面的数量

        //空：数量为1
        if (number==1){
           shoppingCartService.removeById(one1);
            //不为空：数量+1
        }else {
            one1.setNumber(number-1);
            shoppingCartService.updateById(one1);
        }

        return R.success("ok");

    }

    /**
     * 工具类 购物车增加与修改
     * @param shoppingCart
     * @return
     */

    public ShoppingCart getOne1(ShoppingCart shoppingCart){
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        //获取用户id
        shoppingCart.setUserId(BaseContext.getCurrentId());
        queryWrapper.eq(ShoppingCart::getUserId,shoppingCart.getUserId());
        //判空 菜品id
        if (shoppingCart.getDishId()!=null){
            //lam中设置用户id和菜品id
          queryWrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
            System.out.println(1);
        }else{
         queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
            System.out.println(2);
        }


        // 返回之前设置这个对象的UserID

        //返回
        return shoppingCartService.getOne(queryWrapper);

    }


}
